﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;
using System.IO;
using DragonAgeConsoleTrainer.Data.CodeDataTableAdapters;
using DragonAgeConsoleTrainer.Data;
using System.Runtime.InteropServices;

namespace DragonAgeConsoleTrainer
{
	public partial class MainForm : Form
	{
		//	Drives the program, all Commands and Lookup Tables
		CodeData codeData = new CodeData();

		private IntPtr dragonAgeHwnd = IntPtr.Zero;

		[DllImport("user32.dll")]
		static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

		[DllImport("user32.dll")]
		public static extern bool SetForegroundWindow(IntPtr hWnd);

		public MainForm()
		{
			InitializeComponent();
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			if (DesignMode)
				return;

			//	The DataSet xml is an embedded resource, doing this so that
			//	we can have the niceities of a Database without actually
			//	depending on an external database and additional assemblies.
			codeData.ReadXml(Assembly.GetExecutingAssembly().GetManifestResourceStream("DragonAgeConsoleTrainer.Data.dragonage.xml"));

			grdCommands.Codes = codeData.Command;

			//	Hiding CommandText and Argument Columns
			grdCommands.Grid.Columns[2].Visible = false;
			grdCommands.Grid.Columns[3].Visible = false;
			grdCommands.Grid.Columns[4].Visible = false;

			txtArgument.Font = txtConsoleText.Font = Program.FixedWidthFont;
		}


		private void btnExecute_Click(object sender, EventArgs e)
		{
			ExecuteCommand();
		}

		private void ExecuteCommand()
		{
			string consoleText = "`" + txtConsoleText.Text + "\n";

			IntPtr dragonAgeHwnd = FindWindow("DragonAge", "Dragon Age: Origins");
			if (dragonAgeHwnd == IntPtr.Zero)
			{
				MessageBox.Show("Dragon Age Origins Window cannot be found");
				return;
			}
			SetForegroundWindow(dragonAgeHwnd);


			System.Threading.Thread.Sleep(50);
			SendKeys.Send(consoleText);

			this.Activate();
		}

		private string GetConsoleText()
		{
			CodeData.CommandRow command = grdCommands.Code as CodeData.CommandRow;
			if (command == null)
			{
				txtArgument.Text = string.Empty;
				return string.Empty;
			}
			string commandText = string.Format(command.CommandText, GetArgument1(), txtArgument.Text);
			return "runscript " + commandText.Trim();
		}

		private void grdArgument_CodeChanged(object sender, EventArgs e)
		{
			txtConsoleText.Text = GetConsoleText();
		}

		private void txtArgument_TextChanged(object sender, EventArgs e)
		{
			txtConsoleText.Text = GetConsoleText();
		}

		private string GetArgument1()
		{
			if (!grdArgument.Enabled || grdArgument.Code == null)
				return string.Empty;

			return grdArgument.Code[0].ToString();
		}

		private void grdCommands_CodeChanged(object sender, EventArgs e)
		{
			grdArgument.Codes = null;

			if (grdCommands.Code != null)
			{
				CodeData.CommandRow command = grdCommands.Code as CodeData.CommandRow;
				if (!command.IsArgument1Null())
				{
					grdArgument.Codes = codeData.Tables[command.Argument1];
					grdArgument.Enabled = true;
				}
				else
				{
					grdArgument.Enabled = false;
				}

				txtArgument.Enabled = !command.IsArgument2Null();
				txtConsoleText.Text = GetConsoleText();
			}
		}		

		private void grdCommands_Execute(object sender, EventArgs e)
		{
			ExecuteCommand();
		}

		private void grdArgument_Execute(object sender, EventArgs e)
		{
			ExecuteCommand();
		}

		private void ShowHelpAbout()
		{
			new AboutForm().ShowDialog(this);
		}

		private void btnAbout_Click(object sender, EventArgs e)
		{
			ShowHelpAbout();
		}

		private void MainForm_HelpRequested(object sender, HelpEventArgs hlpevent)
		{
			ShowHelpAbout();
		}

		private void chkTransparent_CheckedChanged(object sender, EventArgs e)
		{
			if (chkTransparent.Checked)
				this.Opacity = 0.7d;
			else
				this.Opacity = 1d;
		}

		private void chkTopmost_CheckedChanged(object sender, EventArgs e)
		{
			this.TopMost = chkTopMost.Checked;			
		}
	}
}
